<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let exit = false
    function isEquene(left, right) {
      return left === right
    }
    function returnValue(key, value, result) {
      return key ? (result[key] = value) : (result = value)
    }
    function trivialMatchesDiffFilter(left, right, key, result) {
      if (
        typeof left === 'function' ||
        typeof right === 'function'
      ) {
        throw new Error('functions are not supported');
        return 
      }
      if (typeof left === 'undefined' && typeof right !== 'undefined') {
        return returnValue(key, [right], result)
      }
      if (typeof right === 'undefined') {
        return returnValue(key, [left, 0, 0], result)
      }
      if (typeof left === 'boolean' || typeof left === 'number' || typeof left === 'string') {
        return returnValue(key, [left, right], result)
      }
      const leftType = left === null ? 'null' : typeof left;
      const rightType = right === null ? 'null' : typeof right;
      if (leftType !== rightType) {
        return returnValue(key, [left, right], result)
      }
      filters(left, right, key, result)
    }
    function filters(left, right, key, result) {
      objectsDiffFilter(left, right, key, result)
      // textDiffFilter(left, right, key, result)
    }
    function objectsDiffFilter(left, right, key, result) {
      if (typeof left !== 'object' || typeof right !== 'object') return
      const obj1 = {}
      if(key) {
        result[key] = obj1
      }
      for (const i in left) {
        jsonDiffpatch(left[i], right[i], i, key ? obj1 : result)
      }
      for(const i in right) {
        if(right[i] && typeof left[i] === 'undefined') {
          jsonDiffpatch(undefined, right[i], i, key ? obj1 : result)
        }
      }
    }
    // function textDiffFilter(left, right, key, result) {
    //   if(left !== right) {
    //     result[key] = [left, right]
    //   }
    // }
    function jsonDiffpatch(left, right, key = null, result = {}) {
      if (isEquene(left, right)) return
      const filterValue = trivialMatchesDiffFilter(left, right, key, result)
      // if (!key) return filterValue
      // result[key] = filterValue
      // console.log(key, result);
      // return result
    }
    const a = { a: {b: 2, d: {a: 1}},v: {l: 1} }
    const b = { a: {b:3, c: 4}, v: {l: 1} }
    const obj = {}
    jsonDiffpatch(a, b, null, obj)
    console.log(obj);
  </script>
</body>

</html>